package zte.wb;

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class MessageCodeTrans {
	private static String sourceFilePath = "C:\\Users\\Administrator\\Desktop\\LtError.error.xml";

	private static String targetFilePath = "C:\\Users\\Administrator\\Desktop\\dp.message.xml";

	public static void main(String[] args) throws Exception {
		Document targetFile = new SAXReader().read(targetFilePath);
		Element targetRoot = targetFile.getRootElement();
		for (Object o : new SAXReader().read(sourceFilePath).selectNodes("//error")) {
			Element error = (Element) o;
			Element message = targetRoot.addElement("message");
			String id = error.getParent().attributeValue("id");
			id = error.attribute("id").getValue().substring(0, 1) + id.toUpperCase() + error.attribute("id").getValue().substring(1);
			message.addAttribute("id", id);
			message.addAttribute("type", error.attribute("type").getValue());
			message.addAttribute("message", error.attribute("message").getValue());
		}
		saveFile(targetFile, targetFilePath);
	}

	private static void saveFile(Document document, String fileName) throws Exception {
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("UTF-8");
		XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(fileName)), format);
		writer.write(document);
		writer.flush();
		writer.close();
	}
}
